﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Microsoft.AnalysisServices.AdomdClient;

namespace MDX_Studio_v2
{
	public partial class ResultsWindow : DockContent
	{
		private Microsoft.AnalysisServices.AdomdClient.CellSet _results;	// Results set to be displayed
		private List<string> _messages = new List<string>();				// Set of messages to be displayed

		/// <summary>
		/// Public results accessor
		/// </summary>
		public Microsoft.AnalysisServices.AdomdClient.CellSet Results
		{
			get { return _results; }
			set
			{
				ClearMessages();
				_results = value;
				InitResults();
			}
		}

		/// <summary>
		/// Clear all messages
		/// </summary>
		public void ClearMessages()
		{
			_messages.Clear();
			richBox_Messages.Text = "";
		}

		public void AddDebug(string message)
		{
			richTextBox_Debug.Text += "\r\n" + message;
		}

		/// <summary>
		/// Add a message to the Message window
		/// </summary>
		/// <param name="mess">Message to display</param>
		/// <param name="isError">true -> Message window become selected</param>
		public void AddMessage(string mess, bool isError=true)
		{
			if (isError)
			{
				//ClearMessages();
				tabControl1.SelectedTab = tab_Messages;
			}
			_messages.Add(mess);
			//foreach (string message in _messages) richBox_Messages.AppendText(message + "\r\n");
			richBox_Messages.Lines = _messages.ToArray();
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public ResultsWindow()
		{
			InitializeComponent();
			dataGridView_Results.DefaultCellStyle.NullValue = "(null)";
		}

		/// <summary>
		/// Initialize the results window; display information if no data is returned
		/// </summary>
		private void InitResults()
		{
			dataGridView_Results.Columns.Clear();
			dataGridView_Results.Rows.Clear();

			if (_results.Axes.Count == 2)
			{
				LoadResults();
				tabControl1.SelectedTab = tab_results;
			}
			else
			{
				if (_results.Axes.Count == 0) Console.WriteLine("No results");
				
				else Console.WriteLine("Other");

				tabControl1.SelectedTab = tab_Messages;
			}
		}

		/// <summary>
		/// Load the results into the Results tab
		/// </summary>
		private void LoadResults()
		{
			dataGridView_Results.SuspendLayout();

			Axis columns = _results.Axes[0];
			Axis rows = _results.Axes[1];
			CellCollection values = _results.Cells;


			int rowCount = rows.Positions[0].Members.Count, colCount = columns.Positions.Count;

			// hierarchies
			for (int i = 0; i < rowCount; i++)
				dataGridView_Results.Columns.Add(i.ToString(), "");

			// Values
			for (int i = 0; i < colCount; i++)
				dataGridView_Results.Columns.Add("value" + i, columns.Positions[i].Members[0].Caption);

			int j = 0;
			foreach (Position p in rows.Positions)
			{
				dataGridView_Results.Rows.Add();
				int i = 0;
				foreach (Member m in p.Members)
				{
					//Console.Write(m.Caption + "  ");
					dataGridView_Results.Rows[j].Cells[i].Value = m.Caption;
					i++;
				}

				for(int ii=0; ii<colCount; ii++)
				{
					var val = values[ii, j].Value;	// Used to determine if null
					dataGridView_Results.Rows[j].Cells[i + ii].Value = val == null ? val : values[ii, j].FormattedValue;
				}
				
				
				//Console.WriteLine();
				j++;
			}
			AddMessage("Rows: " + j, false);
			this.dataGridView_Results.AutoResizeColumns();
			this.dataGridView_Results.ResumeLayout();

		}
	}
}
